<?php
/**
 * User: lsx
 * Date: 2017/6/24
 * Time: 11:42
 * Progress:0%
 */

namespace logic\admin\index;
use library\util\Export;
use model\AccountBill;
use think\db;


class LAccountBill
{

    public function index_data($param){
        $model = new AccountBill();
        $keywords=$param['search'];
        $where=array();
        if (!empty($keywords)) {
            $where['ac.nick|m.remark|ac.mobile|ac.wechat'] = array('like', "%{$keywords}%"); //查询条件
        }

        // <editor-fold defaultstate="collapsed" desc="开始时间 截止时间">
        if (!empty($param['begindate'])) {
            $begin_date = strtotime($param['begindate']);
        } else {
            $begin_date = '';
        }
        if (!empty($param['enddate'])) {
            $end_date = strtotime('+1 day', strtotime($param['enddate']));
        } else {
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['m.add_time'] = array('between', "{$begin_date},{$end_date}");
        } else if (!empty($begin_date) && empty($end_date)) {
            $where['m.add_time'] = array('egt', $begin_date);
        } else if (empty($begin_date) && !empty($end_date)) {
            $where['m.add_time'] = array('elt', $end_date);
        }
        // </editor-fold>

        $total=$model->alias('m')->join('account ac','ac.id=m.account_id')->where($where)->count('m.id'); //记录总数
        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['m.add_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model->alias('m')
            ->join('account ac','ac.id=m.account_id')
            ->field('m.*,ac.nick,ac.mobile,ac.wechat')
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;

    }

    public function logic_export($params){
        $keywords=$params['search'];
        $where=array();
        if (!empty($keywords)) {
            $where['a.nick|m.remark|a.mobile|a.wechat'] = array('like', "%{$keywords}%"); //查询条件
        }

        // <editor-fold defaultstate="collapsed" desc="开始时间 截止时间">
        if (!empty($params['begindate'])) {
            $begin_date = strtotime($params['begindate']);
        } else {
            $begin_date = '';
        }
        if (!empty($params['enddate'])) {
            $end_date = strtotime('+1 day', strtotime($params['enddate']));
        } else {
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['aro.add_time'] = array('between', "{$begin_date},{$end_date}");
        } else if (!empty($begin_date) && empty($end_date)) {
            $where['aro.add_time'] = array('egt', $begin_date);
        } else if (empty($begin_date) && !empty($end_date)) {
            $where['aro.add_time'] = array('elt', $end_date);
        }
        // </editor-fold>
        $sort['aro.add_time'] = 'desc'; //默认排序
        $rows = Db::name('account_bill')->alias('aro')
            ->join('account a','a.id=aro.account_id')
            ->join('shop_user u','a.id=u.account_id')
            ->join('shop_user_group g','g.id=u.group_id')
            ->field("a.nick,a.name,a.mobile,g.name as group_name,aro.money,FROM_UNIXTIME(aro.add_time,'%Y-%m-%d %H:%i:%s') as add_time,aro.remark")
            ->where($where)
            ->order($sort)
            ->select();

        $xlsName  = "外卖--充值记录";
        $xlsCell  = array(
            array('nick','昵称'),
            array('name','姓名'),
            array('mobile','手机号'),
            array('group_name','会员分组'),
            array('money','金额'),
            array('add_time','时间'),
            array('remark','备注'),
        );
        $xlsData  = $rows;
        Export::excel($xlsCell,$xlsData,$xlsName);
    }
}